package com.skyline.wo.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.skyline.base.dao.impl.BaseDaoImpl;
import com.skyline.base.type.Activity;
import com.skyline.common.bean.Page;
import com.skyline.common.cache.annotation.Cache;
import com.skyline.common.cache.annotation.CacheCategoryType;
import com.skyline.common.cache.annotation.CacheDelete;
import com.skyline.common.cache.annotation.Fk;
import com.skyline.common.cache.annotation.Param;
import com.skyline.wo.dao.ArticleDao;
import com.skyline.wo.mapper.ArticleMapper;
import com.skyline.wo.mapper.ArticleSimpleMapper;
import com.skyline.wo.model.Article;

/**
 * AricleDaoImpl is the implement of the Article DATABASE ACCESS operation
 * 
 * @author Jairus Chan
 * @version 0.1
 * 
 */
@Repository("articleDao")
public class ArticleDaoImpl extends BaseDaoImpl implements ArticleDao {

	/**
	 * insert into
	 * article(authority,ownerId,ownerPortrait,ownerNickname,createTime
	 * ,updateTime
	 * ,commentNum,shareNum,activity,categoryId,categoryName,title,content
	 * ,digest,readNum)
	 * values(?,?,?,?,current_timestamp,current_timestamp,0,0,?,?,?,?,?,?,0)
	 */
	@Value("${ArticleDao.insertArticle}")
	private String insertArticleSql;

	/**
	 * select
	 * id,authority,ownerId,ownerPortrait,ownerNickname,createTime,updateTime
	 * ,commentNum,shareNum,categoryId,categoryName,title,digest,readNum from
	 * article where ownerId=? and authority<? and activity=?
	 */
	@Value("${ArticleDao.queryArticleByOwnerId}")
	private String queryArticleByOwnerIdSql;

	@Value("${ArticleDao.queryArticleByCategoryId}")
	private String queryArticleByCategoryIdSql;

	@Value("${ArticleDao.queryArticleByArticleId}")
	private String queryArticleByArticleIdSql;

	@Value("${ArticleDao.updateArticleVisitNum}")
	private String updateArticleVisitNumSql;

	@Value("${ArticleDao.deleteArticle}")
	private String deleteArticleSql;
	
	@Value("${ArticleDao.updateArticle}")
	private String updateArticleSql;
	
	@Value("${ArticleDao.queryPriorArticleById}")
	private String queryPriorArticleByIdSql;
	
	@Value("${ArticleDao.queryNextArticleById}")
	private String queryNextArticleByIdSql;

	@Override
	@CacheDelete(type=CacheCategoryType.ARTICLE)
	public long insertArticle(Integer authority, @Fk Long ownerId, String ownerPortrait, String ownerNickname,
			String title, String content, String digest, Long categoryId, String categoryName) {
		/*
		 * String digest = content; digest=digest.replaceAll("&nbsp;", ""); if
		 * (digest.length() > 1000) { digest=digest.substring(0, 1000); } digest
		 * = StringUtils.html2text(digest); if (digest.length() >= 600) {
		 * digest=digest.substring(0, 600); }
		 */
		return insertWithIdReturn(insertArticleSql, authority, ownerId, ownerPortrait, ownerNickname,
				Activity.NORMAL.toString(), categoryId, categoryName, title, content, digest);
		//jdbcTemplate.update(insertArticleSql, authority, ownerId, ownerPortrait, ownerNickname,
//			/	Activity.NORMAL.toString(), categoryId, categoryName, title, content, digest);
	}
	
	@Override
	@CacheDelete(type=CacheCategoryType.ARTICLE)
	public void updateArticle(Long id,Integer authority,@Fk Long ownerId, 
			String title, String content, String digest, Long categoryId, String categoryName){
		jdbcTemplate.update(updateArticleSql,authority, categoryId, categoryName, title, content, digest,id,ownerId);
	}

	@Override
	public void updateArticleAutyOrCateg(Integer id, Integer authority, Long categoryId, String categoryName) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateArticleContent(Integer id, Integer authority, String ownerPortrait, String ownerNickname,
			String title, String content, Long categoryId, String categoryName) {
		// TODO Auto-generated method stub

	}

	@Override
	@Cache(keyPattern = "article-l-:uid-auth:auth", type = CacheCategoryType.ARTICLE, pagination = true)
	public List<Article> queryArticleByOwnerId(Page page, @Param("uid") Long ownerId, @Param("auth")  Integer authority) {
		List<Article> articles = getPaginationResult(queryArticleByOwnerIdSql, page, ArticleSimpleMapper.getMapper(),
				ownerId, authority);
		return articles;
	}

	@Override
	@Cache(keyPattern = "article-l-:cid-:uid-auth:auth", type = CacheCategoryType.ARTICLE, pagination = true)
	public List<Article> queryArticleByCategoryId(Page page, @Param("cid")Long categoryId,  @Param("uid") Long ownerId,  @Param("auth")  Integer authority) {
		List<Article> articles = getPaginationResult(queryArticleByCategoryIdSql, page,
				ArticleSimpleMapper.getMapper(), categoryId,ownerId, authority);
		return articles;
	}

	@Override
	@Cache(keyPattern = "article-:id", type = CacheCategoryType.ARTICLE)
	public Article queryArticleById(@Param("id")  Long id) {
		List<Article> articles = jdbcTemplate.query(queryArticleByArticleIdSql, ArticleMapper.getMapper(), id);
		if(articles.size()>0)
			return articles.get(0);
		else return null;
		//return article;
	}

	@Override
	@CacheDelete(type=CacheCategoryType.ARTICLE)
	public void deleteArticle(Long id, @Fk Long actionerId) {
		jdbcTemplate.update(deleteArticleSql, id, actionerId);

	}

	@Override
	//@CacheDelete(type=CacheCategoryType.ARTICLE)
	public void updateArticleVisitNum( Long id) {
		jdbcTemplate.update(updateArticleVisitNumSql, id);
	}
	
	@Override
	@Cache(keyPattern = "article-prior-:id-auth:auth", type = CacheCategoryType.ARTICLE)
	public Article queryPriorArticleById(Long categoryId,@Param("id") Long id,@Param("auth") Integer authority){ 
		List<Article> articles =jdbcTemplate.query(queryPriorArticleByIdSql,ArticleMapper.getMapper(),categoryId, id,authority);
		if(articles.size()>0)
			return articles.get(0);
		else return null;
	}
	
	@Override
	@Cache(keyPattern = "article-next-:id-auth:auth", type = CacheCategoryType.ARTICLE)
	public Article queryNextArticleById(Long categoryId,@Param("id")  Long id,@Param("auth") Integer authority){
		List<Article> articles =jdbcTemplate.query(queryNextArticleByIdSql,ArticleMapper.getMapper(),categoryId, id,authority);
		if(articles.size()>0)
			return articles.get(0);
		else return null;
	}

}
